import { defHttp } from "/@/utils/http/axios"
import { Modal } from "ant-design-vue"

enum Api {
  list = "/infra-service/job/page",
  listTaskBean = "/infra-service/job/listTaskBean",
  save = "/infra-service/job/save",
  edit = "/infra-service/job/edit",
  get = "/infra-service/job/getById",
  pause = "/infra-service/job/pause",
  resume = "/infra-service/job/resume",
  delete = "/infra-service/job/delete",
  exportXlsUrl = "/infra-service/job/exportXls",
  importExcelUrl = "/infra-service/job/importExcel",
  execute = "/infra-service/job/execute",
  deleteBatch = "/infra-service/job/deleteBatch",
}

/**
 * 导出api
 */
export const getExportUrl = Api.exportXlsUrl
/**
 * 导入api
 */
export const getImportUrl = Api.importExcelUrl
/**
 * 查询任务列表
 * @param params
 */
export const getQuartzList = (params) => {
  return defHttp.get({ url: Api.list, params })
}
/**
 * 获取任务bean的名字
 * @param params
 */
export const listTaskBean = () => {
  return defHttp.get({ url: Api.listTaskBean })
}

/**
 * 保存或者更新任务
 * @param params
 */
export const saveOrUpdateQuartz = (params, isUpdate) => {
  const url = isUpdate ? Api.edit : Api.save
  return defHttp.post({ url: url, params })
}

/**
 * 查询任务详情
 * @param params
 */
export const getQuartzById = (params) => {
  return defHttp.get({ url: Api.get, params })
}

/**
 * 删除任务
 * @param params
 */
export const deleteQuartz = (params, handleSuccess) => {
  return defHttp.delete({ url: Api.delete, data: params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess()
  })
}

/**
 * 启动
 * @param params
 */
export const resumeJob = (params, handleSuccess) => {
  return defHttp.get({ url: Api.resume, params }).then(() => {
    handleSuccess()
  })
}

/**
 * 暂停
 * @param params
 */
export const pauseJob = (params, handleSuccess) => {
  return defHttp.get({ url: Api.pause, params }).then(() => {
    handleSuccess()
  })
}

/**
 * 立即执行
 * @param params
 */
export const executeImmediately = (params, handleSuccess) => {
  return defHttp.put({ url: Api.execute, params }).then(() => {
    handleSuccess()
  })
}

/**
 * 批量删除任务
 * @param params
 */
export const batchDeleteQuartz = (params, handleSuccess) => {
  Modal.confirm({
    title: "确认删除",
    content: "是否删除选中数据",
    okText: "确认",
    cancelText: "取消",
    onOk: () => {
      return defHttp.delete({ url: Api.deleteBatch, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess()
      })
    },
  })
}
